import 'package:flutter/material.dart';

main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: LJHomePage(),
    );
  }
}

class LJHomePage extends StatelessWidget {
  const LJHomePage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Center(
          child: Text("商品列表"),
        ),
      ),
      body: LJHomeContent("你好 赖家健"),
    );
  }
}

/*
* 把StatelessWidget 转成 StatefulWidget 快捷键 alt + enter
* 抽出widget alt+shift+.
* */

class LJHomeContent extends StatefulWidget {
  // const LJHomeContent({Key key}) : super(key: key);
  final String message;
  LJHomeContent(this.message);
  @override
  _LJHomeContentState createState() => _LJHomeContentState();
}

//_只允许自己使用
class _LJHomeContentState extends State<LJHomeContent> {
  int _count = 0;
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          _getButtons(),
          Test(),
          Text("当前计数$_count",style: TextStyle(fontSize: 25)),
          Text("传过来的message ${ this.widget.message }")
        ],
      ),
    );
  }
  Widget _getButtons (){
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        ElevatedButton(onPressed:(){
          setState(() {
            _count++;
          });
        }, child: Icon(Icons.add)),
        ElevatedButton(onPressed: (){
          setState(() {
            _count--;
          });
        }, child: Icon(Icons.remove))
      ],
    );
  }
}

//StatefulWidget 生命周期
class Test extends StatefulWidget {
  // const Test({Key key}) : super(key: key);

  Test(){
    print("1.调用Test的constructor");
  }

  @override
  _TestState createState(){
    print("2.调用Test的createState方法");
    return _TestState();
  }
}

class _TestState extends State<Test> {
  _TestState(){
    print("3.调用_TestState的constructor方法");
  }

  @override
  void initState() {
    // TODO: implement initState
    print("4.调用initState方法");
    super.initState();
  }

  @override
  void didUpdateWidget(covariant Test oldWidget) {
    // TODO: implement didUpdateWidget
    print("5.调用didUpdateWidget");
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    return Text("测试生命周期");
  }
}